<!DOCTYPE html>
<html>
  <head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
	<title>Polkadot node</title>
	<link rel="shortcut icon" href="/favicon.png" />
	<script type="module">
import { start_client, default as init } from './pkg/polkadot_cli.js';

function log(msg) {
	document.getElementsByTagName('body')[0].innerHTML += msg + '\n';
}

async function start() {
	log('Loading WASM');
	await init('./pkg/polkadot_cli_bg.wasm');
	log('Successfully loaded WASM');
	log('Fetching chain spec');
	const chain_spec_response = await fetch("https://raw.githubusercontent.com/paritytech/polkadot/master/node/service/res/westend.json");
	const chain_spec_text = await chain_spec_response.text();

	// Build our client.
	log('Starting client');
	let client = await start_client(chain_spec_text, 'info');
	log('Client started');

	client.rpcSubscribe('{"method":"chain_subscribeNewHead","params":[],"id":1,"jsonrpc":"2.0"}',
		(r) => log("New chain head: " + r));

	setInterval(() => {
		client
			.rpcSend('{"method":"system_networkState","params":[],"id":1,"jsonrpc":"2.0"}')
			.then((r) => log("Network state: " + r));
	}, 20000);
}

start();
	</script>
  </head>
  <body style="white-space: pre"></body>
</html>
